﻿package  com.athena.controller
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import com.adobe.serialization.json.JSON;
	import com.athena.process.MailFunction ;
	import com.athena.process.BasicFunction ;
	/**
	 * ...
	 * @author Tamnt
	 */
	public class MailItemController 
	{
		var movie_mc:MovieClip;
		var objMain : Object ;
		
		public function MailItemController(_mc:MovieClip, _obj : Object) 
		{
			movie_mc = _mc;
			objMain = _obj ;			
			movie_mc.Content_txt.addEventListener(MouseEvent.CLICK, click);
			movie_mc.Delete_chk.addEventListener(MouseEvent.CLICK, Delete_Click);	
		}
		public function Delete_Click(evt:MouseEvent):void {
			if ((objMain.T == 0) || (objMain.T == 4) || (objMain.T == 5))  {
				for (var i:int = 0; i < MailFunction.arrMailAdmin.length; i++) 
				{
					if (MailFunction.arrMailAdmin[i].Id == objMain.Id) {
						MailFunction.arrMailAdmin[i].chk = movie_mc.Delete_chk.selected ;
						break ;
					}
				}
			} else if (objMain.T == 1) {
				for (var j:int = 0; j < MailFunction.arrMailTransfer.length; j++) 
				{
					if (MailFunction.arrMailTransfer[j].Id == objMain.Id) {
						MailFunction.arrMailTransfer[j].chk = movie_mc.Delete_chk.selected ;
						break ;
					}
				}
			} else {
				for (var j:int = 0; j < MailFunction.arrMailPlayer.length; j++) 
				{
					if (MailFunction.arrMailPlayer[j].Id == objMain.Id) {
						MailFunction.arrMailPlayer[j].chk = movie_mc.Delete_chk.selected ;
						break ;
					}
				}
			}	
		}
		public function click(evt:MouseEvent):void
		{
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Mail")) ;
			if (mc == null) return ;
			var AG : Number = 0 ;
			var id : Number = 0 ;
			if ((objMain.T == 0) || (objMain.T == 4) || (objMain.T == 5))  {
				for (var i:int = 0; i < MailFunction.arrMailAdmin.length; i++) 
				{
					if (MailFunction.arrMailAdmin[i].Id == objMain.Id) {
						if (MailFunction.arrMailAdmin[i].S == false) {
							MailFunction.arrMailAdmin[i].S = true ;
							Cocaro.currPlayer.CountMsg-- ;
						}
						AG = MailFunction.arrMailAdmin[i].AG ;
						id = MailFunction.arrMailAdmin[i].Id
						break ;
					}
				}
			} else if (objMain.T == 1) {
				for (var j:int = 0; j < MailFunction.arrMailTransfer.length; j++) 
				{
					if (MailFunction.arrMailTransfer[j].Id == objMain.Id) {
						if (MailFunction.arrMailTransfer[j].S == false) {
							MailFunction.arrMailTransfer[j].S = true ;
							Cocaro.currPlayer.CountMsg-- ;
						}
						AG = MailFunction.arrMailTransfer[j].AG ;
						id = MailFunction.arrMailTransfer[j].Id ;
						break ;
					}
				}
			} else {
				for (var l:int = 0; l < MailFunction.arrMailPlayer.length; l++) 
				{
					if (MailFunction.arrMailPlayer[l].Id == objMain.Id) {
						if (MailFunction.arrMailPlayer[l].S == false) {
							MailFunction.arrMailPlayer[l].S = true ;
							Cocaro.currPlayer.CountMsg-- ;
						}
						AG = MailFunction.arrMailPlayer[l].AG ;
						id = MailFunction.arrMailPlayer[l].Id ;
						break ;
					}
				}
			}
			mc.MailDetail_mc.From_txt.text = objMain.From ;
			mc.MailDetail_mc.Content_txt.text = objMain.Msg ; 
			mc.MailDetail_mc.Time_txt.text = BasicFunction.convertTimeToString(objMain.Time) ;
			mc.MailDetail_mc.Id_txt.text = objMain.Id ;
			mc.MailDetail_mc.AG_txt.text = BasicFunction.numberFormat(AG, 0, false, true) ;
			mc.MailDetail_mc.Reply_btn.visible = true ;
			if (AG > 0) 
				mc.MailDetail_mc.ReceiveAG_btn.visible = true ;
			else 
				mc.MailDetail_mc.ReceiveAG_btn.visible = false ;
			var formatText = new TextFormat() ;				
			formatText.font = "Arial" ;
			formatText.bold = false ;
			formatText.color = 0xE5DFE9 ;
			formatText.size = 12 ;
			movie_mc.Content_txt.setTextFormat(formatText) ;
			if (mc.Send_mc.visible) {
				mc.Send_mc.visible = false ;
				mc.MailDetail_mc.visible = true ;
			}
			objMain.S = true ;
					
			var obj :Object = new Object() ;
			obj.evt = "15" ;
			obj.T = 1 ;
			obj.ID = objMain.Id ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;			
		}
	}
}